package Gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import EventManager.EventManager;
import Main.Defines;

public class GameFrame extends JFrame implements ActionListener
{
	public static GameFrame ref;
	private JMenuBar gameMenuBar;
	private JMenuItem gameNewMenuItem;
	private JMenuItem gameAboutItem;
	private JMenuItem gameInstructionsItem;
	private JMenuItem gameExitMenuItem;
	private JMenuItem gameOptionsItem;
	
	/**
	 * Creates an GameFrame Singleton
	 * @return GameFrame Object
	 */
	public static GameFrame getInstance()
	{
		if (ref == null)
			// it's ok, we can call this constructor
			ref = new GameFrame();
		return ref;
	}

	/**
	 * Constructor
	 * @param Size
	 *            (800,600)
	 * @param Resizable
	 *            (False)
	 * @param Title
	 *            ("PayDay! with Java")
	 */
	private static final int FRAME_WIDTH = 700;
	private static final int FRAME_HEIGHT = 660;
	public Dimension getMinimumSize(){	
		return new Dimension(FRAME_WIDTH,FRAME_HEIGHT); 
		// MAGIC NUMBERS - USE THE SCREEN WIDTH AND HEIGHT VARIABLES NUB
	}
	public Dimension getPreferredSize(){
		return new Dimension(FRAME_WIDTH,FRAME_HEIGHT);
	}
	public Dimension getMaximumSize(){
		return new Dimension(FRAME_WIDTH,FRAME_HEIGHT);
	}
	public GameFrame()
	{
		super("PayDay! with Java");
		setResizable(false);
		
		// make the frame half the height and width
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height;
		int width = screenSize.width;
		setSize(width / 2, height / 2);
		setLocationRelativeTo(null);

		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		// Creates the menu
		buildMenu();
		pack();
		
		setVisible(true);
		
	}
	public void update(){
		// if game is live
		GamePanel.getInstance().update();
		pack();
	}
	private static final long serialVersionUID = -5674122410142457138L;

	/**
	 * Handles Actions passed into the GameFrame
	 */
	public void actionPerformed(ActionEvent actionevent)
	{
		/**
		 * Exits the game
		 */
		if (actionevent.getSource() == gameExitMenuItem)
		{
			System.exit(0); // REPLACE WITH m_running = false
		}
		else if (actionevent.getSource() == gameNewMenuItem)
		{
			EventManager.getInstance().queueEvent("EVENT_START_GAME");
		}
		else if (actionevent.getSource() == gameOptionsItem)
		{
			EventManager.getInstance().queueEvent("EVENT_OPTIONS_MENU");
		}
		else if (actionevent.getSource() == gameInstructionsItem)
		{
			EventManager.getInstance().queueEvent("EVENT_INSTRUCTIONS_MENU");
		}
		else if (actionevent.getSource() == gameAboutItem)
		{
			EventManager.getInstance().queueEvent("EVENT_ABOUT_LOAD");
		}
		
	}
	/**
	 * Creates the Menubar
	 */
	private void buildMenu()
	{
		gameMenuBar = new JMenuBar();
		setJMenuBar(gameMenuBar);
		JMenu jmenu = new JMenu("Game");
		jmenu.setMnemonic(KeyEvent.VK_G);
		
		gameNewMenuItem = new JMenuItem("New Game", KeyEvent.VK_N);
		
		gameNewMenuItem.addActionListener(this);
		jmenu.add(gameNewMenuItem);
		gameOptionsItem = new JMenuItem("Options", KeyEvent.VK_P);
		
		gameOptionsItem.addActionListener(this);
		jmenu.add(gameOptionsItem);
		gameExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
		gameExitMenuItem.addActionListener(this);
		jmenu.add(gameExitMenuItem);
		gameMenuBar.add(jmenu);
		JMenu jmenu2 = new JMenu("Help");
		jmenu2.setMnemonic(KeyEvent.VK_H);
		gameInstructionsItem = new JMenuItem("Instructions",KeyEvent.VK_I);
		gameInstructionsItem.addActionListener(this);
		jmenu2.add(gameInstructionsItem);
		gameAboutItem = new JMenuItem("About",KeyEvent.VK_A);
		gameAboutItem.addActionListener(this);
		jmenu2.add(gameAboutItem);
		
		gameMenuBar.add(jmenu2);
	}

}